package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageReturn;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.vo.YddAdvimgVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 广告管理（后台）/4.2、广告图管理
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    /**
     * 广告图详情(用于修改回显数据)
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id) {
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        return ResultData.ok("", yddAdvimg);
    }

    /**
     * 广告图分页查询
     *
     * @param yddAdvimgDto
     * @return
     */
    @GetMapping("/list")
    public ResultData list(YddAdvimgDto yddAdvimgDto) {
        //分页拦截器
        Page<YddAdvimg> page = new Page<>(yddAdvimgDto.getPage(), yddAdvimgDto.getLimit());
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(yddAdvimgDto.getAdvimgdesc()), YddAdvimg::getAdvimgdesc, yddAdvimgDto.getAdvimgdesc());
        queryWrapper.eq(StringUtils.isNotEmpty(yddAdvimgDto.getAdvimgpos()), YddAdvimg::getAdvimgpos, yddAdvimgDto.getAdvimgpos());
        //查询
        Page<YddAdvimgVo> yddAdvimgVoPage = yddAdvimgService.pageByCondition(page, queryWrapper);
        //返回数据封装
        PageReturn<YddAdvimgVo> res = PageReturn.change(yddAdvimgVoPage);
        return ResultData.ok("", res);
    }

    /**
     * 广告图修改
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvimg yddAdvimg) {
        yddAdvimgService.updateById(yddAdvimg);
        return ResultData.ok("广告图修改成功", null);
    }

    /**
     * 广告图删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(int id) {
        yddAdvimgService.removeById(id);
        return ResultData.ok("广告图删除成功", null);
    }

    /**
     * 广告图添加
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping ("/add")
    public ResultData add(@RequestBody YddAdvimg yddAdvimg) {
        yddAdvimgService.save(yddAdvimg);
        return ResultData.ok("广告图添加成功", null);
    }

}
